package Market;

import java.io.*;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Ratuil
 * Date: 06.03.13
 * Time: 17:01
 * To change this template use File | Settings | File Templates.
 */
public class MarketMain {
    Market market;
    Scanner sc=new Scanner(System.in);
    private static final char EXIT_SYMBOL = '0';
    private static final int NUMBER_OPTIONS = 13;
    public static void main(String[] args) {
        new MarketMain().run();
    }

    public void run(){
        init();
        String s=null;
        int key=0;
        String tmp=null;
        printMenu();
        while(true){
            while(true){
                try{
                    key=Integer.parseInt(sc.nextLine());
                    if(key<0 || key>NUMBER_OPTIONS)throw new Exception();
                    break;
                }catch (Exception e){
                    System.out.println("Error input value:"+key+". Try once more or exit");
                    continue;
                }
            }
            switch(key){
                case 0:market.saveDataSerializable();
                    break;
                case 1:market.getCategories();
                    break;
                case 2:getProducts();
                    break;
                case 3:bye();
                    break;
                case 4:market.getEmployees();
                    break;
                case 5:addCategory();
                    break;
                case 6:addProduct();
                    break;
                case 7:addEmployee();
                    break;
                case 8:removeCategory();
                    break;
                case 9:removeProduct();
                    break;
                case 10:removeEmployee();
                    break;
                case 11:market.getSelling();
                    break;
                case 12:market.top10();
                    break;
                case 13:market.bestEmployee();
            }
            if(key==0)break;
            printMenu();
        }
    }


    public void addSomeData(){
        market.addEmployee("Andrey",5000,2000,1,1);
        market.addEmployee("Artem",2000,2000,6,1);
        market.addEmployee("Kolya",1000,2001,1,1);
        market.addEmployee("Yana",1000,2001,1,1);

        market.addCategory("CPU");
        market.addCategory("MB");
        market.addCategory("GPU");
        market.addCategory("Memory");

        market.addProduct("CPU","i3",1000,50);
        market.addProduct("CPU","i5",1500,50);
        market.addProduct("CPU","i7",2700,50);
        market.addProduct("CPU","athlon",700,50);
        market.addProduct("CPU","sempron",900,50);
        market.addProduct("CPU","atom",300,50);

        market.addProduct("MB","AsusP8B75",660,50);
        market.addProduct("MB","AsusP8Z77",940,50);
        market.addProduct("MB","AsusF2A85",600,50);

        market.addProduct("GPU","HD7850",1800,5);
        market.addProduct("GPU","GTX650",970,5);
        market.addProduct("GPU","GTX660",2000,5);

        market.addProduct("Memory","Kingston4096MB",200,5);
        market.addProduct("Memory","Seagate3TB",1000,5);
        market.addProduct("Memory","Crucial128GBSSD",970,5);
    }
    public void trade(){
        market.sell("Kolya","CPU","i3",15);
        market.sell("Artem","CPU","i5",14);
        market.sell("Yana","CPU","i7",13);
        market.sell("Andrey","CPU","athlon",12);
        market.sell("Kolya","CPU","sempron",11);
        market.sell("Artem","MB","AsusP8B75",10);
        market.sell("Yana","MB","AsusP8Z77",9);
        market.sell("Andrey","MB","AsusF2A85",8);
        market.sell("Kolya","GPU","HD7850",7);
        market.sell("Artem","GPU","GTX650",6);
        market.sell("Yana","GPU","GTX660",5);
        market.sell("Andrey","Memory","Kingston4096MB",4);
        market.sell("Kolya","Memory","Seagate3TB",3);
        market.sell("Artem","Memory","Crucial128GBSSD",2);
    }

    private void init(){
        market=new Market();
        if(market.isEmpty){
            addSomeData();
            trade();
        }

    }

    private void printMenu(){
        printDelimeter();
        System.out.println(
                "Type: 1 - getCategories, 2 - getProducts,3 - BUY,4 - getEmployees,\n" +
                "5 - addCategory, 6 - addProduct, 7 - addEmployee,\n" +
                "8 - removeCategory, 9 - removeProduct, 10 - removeEmployee, \n" +
                "11 - getSeling, 12 - Top10Products, 13 - BestEmployee," +
                "0 - to SAVE and EXIT"
        );
        printDelimeter();
    }

    private void getProducts() {
        System.out.println("Enter categore name");
        String s=sc.nextLine();
        market.getProducts(s);
    }

    private void bye(){
        String employee;
        String category;
        String product;
        int number;

        System.out.println("Enter sellers name:");
        employee = sc.nextLine();
        if(!market.isEmployee(employee)){
            System.out.println("No such Employee");
            return;
        }
        System.out.println("Enter Category name");
        category = sc.nextLine();
        if(!market.isCategory(category)){
            System.out.println("No such category");
            return;
        }
        System.out.println("Enter Product name");
        product = sc.nextLine();
        if(!market.isProduct(category,product)){
            System.out.println("No such product in category");
            return;
        }
        System.out.println("Enter number of products you want to bye");
        try{
            number = sc.nextInt();
            if(number<=0)throw new Exception("number must be positive");
            if(!market.isEnoughNumber(category,product,number))
                throw new Exception("not enough such product");
        }catch(Exception e){
            System.out.println("Input value is incorrect: "+e);
            return;
        }
        MarketStore.Product p=market.getProduct(category, product);
        if(p.equals(null)){System.out.println("No product");return;}
        double bill=p.getPrice()*number;
        System.out.println("Buy accepted, please you have to pay $"+bill);
        boolean isPayed=false;
        isPayed = true;
        if(isPayed){
            market.sell(employee,category,product,number);
            p.setNumber(p.getNumber()-number);
            System.out.println("Buy successful");
        }
    }



    public void addCategory(){
        System.out.println("Enter name of new category:");
        market.addCategory(sc.nextLine());
    }
    public void addProduct(){
        System.out.println("Enter name of category:");
        String category=sc.nextLine();
        if(!market.isCategory(category)){System.out.println("No such category");return;}
        System.out.println("Enter name of product:");
        String product=sc.nextLine();
        System.out.println("Enter price of product");
        double price;
        while(true){
            try{
                price=sc.nextDouble();
                if(price<=0)throw new Exception("Or num must be positive");
                break;
            }catch(Exception e){
                System.out.println("Input val is no number. "+e);
                continue;
            }

        }
        System.out.println("Enter number of product");
        int number;
        while(true){
            try{
                number=sc.nextInt();
                if(number<=0)throw new Exception("Or num must be positive");
                break;
            }catch(Exception e){
                System.out.println("Input val is no number. "+e);
                continue;
            }
        }
        market.addProduct(category,product, price, number);
        System.out.println("New product successfully added");
    }

    public void addEmployee(){
        System.out.println("Enter name of employee");
        String  name=sc.nextLine();
        System.out.println("Enter salary of employee");
        double salary;
        while(true){
            try{
                salary=sc.nextDouble();
                if(salary<=0)throw new Exception("Or salary must be positive");
                break;
            }catch(Exception e){
                System.out.println("Input val is no number. "+e);
                continue;
            }
        }
        int year,month,day;
        while(true){
            try{
                System.out.println("Enter year:");
                year=sc.nextInt();
                System.out.println("Enter month:");
                month=sc.nextInt();
                System.out.println("Enter day:");
                day=sc.nextInt();
                if(year<=1980)throw new Exception("Or year must be > 1980");
                if(month<1 && month>12)throw new Exception("Or month must be from 1 to 12");
                if(day<1 && day>31)throw new Exception("Or day must be from 1 to 31");
                GregorianCalendar calendar=new GregorianCalendar(year, month-1, day);
                break;
            }catch(Exception e){
                System.out.println("Input date is incorrect. "+e);
                continue;
            }
        }
        market.addEmployee(name, salary, year, month, day);
        System.out.println("New employee successfully added");
    }

    public void removeCategory(){
        System.out.println("Enter category name to remove:");
        String category=sc.nextLine();
        market.removeCategory(category);
    }

    public void removeProduct(){
        System.out.println("Enter category name where to remove:");
        String category=sc.nextLine();
        if(!market.isCategory(category)){System.out.println("No such category");return;}
        System.out.println("Enter product name to remove:");
        String product=sc.nextLine();
        market.removeProductAll(category, product);
    }

    public void removeEmployee(){
        System.out.println("Enter index of employee to remove");
        int index;
        while(true){
            try{
                index=sc.nextInt();
                if(index<0)throw new Exception("Or index must be positive");
                break;
            }catch(Exception e){
                System.out.println("Input index is no number. "+e);
                continue;
            }
        }
        market.removeEmployee(index);
    }

    private void printDelimeter(){
        for(int i=0;i<50;i++) System.out.print("-");
        System.out.print("\n");
    }
}

